<template>
  <div class="app-container">
    <div class="filter-div">
      <el-input
        v-model="listQuery.tel"
        placeholder="手机号"
        style="width: 180px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.status" style="width: 120px;" placeholder="请选择状态" @change="handleFilter">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        检索
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-document"
        @click="dialogExcelVisible = true"
      >
        导入
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      max-height="500px"
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        min-width="10%"
        align="center"
      />
      <el-table-column label="手机号" min-width="40%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.tel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="20%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.status === 1 ? "在线":"离线" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="30%">
        <template slot-scope="{row}">
          <el-button v-if="row.status === 0" size="small" type="primary" @click="login(row)">上线</el-button>
          <span v-else> - </span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchList"
    />
    <el-dialog :visible.sync="dialogExcelVisible" style="width:1500px;" title="批量导入账号">
      <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
      <span slot="footer" class="dialog-footer">
        <el-link type="primary" target="_blank" :href="telsTemplateLink">下载模板</el-link>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, importExcel } from '@/api/customer'
import { Message } from 'element-ui'
import UploadExcelComponent from '@/components/UploadExcel/index.vue'

export default {
  name: 'Customer',
  components: { Pagination, UploadExcelComponent },
  data() {
    return {
      statusOptions: [
        { label: '全部', value: -1 },
        { label: '在线', value: 1 },
        { label: '离线', value: 0 }
      ],
      dialogExcelVisible: false,
      listLoading: false,
      total: 0,
      list: [],
      listQuery: {
        tel: '',
        status: -1,
        page: 1,
        limit: 50
      },
      telsTemplateLink: `${process.env.VUE_APP_BASE_API}/xlsxtemplates/telsTemplate.xlsx`
    }
  },
  created() {
    //  从商店页面跳转过来的
    this.listQuery = { ...this.listQuery, store_id: this.$route.query.store_id }
    this.fetchList()
  },
  methods: {
    fetchList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.fetchList()
    },
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1
      if (isLt1M) { return true }
      this.$message({
        message: '不能上传超过1M的文件',
        type: 'warning'
      })
      return false
    },
    handleSuccess({ results, header }) {
      if (header[0] === '手机号') {
        for (let i = 0; i < results.length; i++) {
          const tel = results[i]['手机号']
          if (!/^\d+$/.test(tel)) {
            Message.error(`${tel}不是存数字,请修改后重新导入`)
            return
          }
          if (`${tel}`.length !== 11) {
            Message.error(`${tel}不是11位,请修改后重新导入`)
            return
          }
        }
        importExcel(results).then(response => {
          Message.success('导入成功')
          this.dialogExcelVisible = false
          this.fetchList()
        })
      } else {
        Message({
          message: '请使用模板',
          type: 'error',
          duration: 5 * 1000
        })
      }
    }
  }
}
</script>

<style scoped>
    .filter-div{
        padding-bottom: 20px;
    }
</style>
